#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
 
// 设置wifi接入信息(请根据您的WiFi信息进行修改)
const char* ssid = "wifi名字";
const char* password = "wifi密码";
const char* STAip = "192.168.184.149";

WiFiUDP Udp;
// 复位或上电后运行一次:
void setup() {
  //Serial.setRxBufferSize(20000);
  //Serial.begin(2000000);  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {      // WiFi.status()函数的返回值是由NodeMCU的WiFi连接状态所决定的。 
    delay(1000);                               // 如果WiFi连接成功则返回值为WL_CONNECTED                       
  }
  Serial.setRxBufferSize(10000);
  Serial.begin(2000000); 
}

size_t RxHeadState = 0;
size_t RxTailState = 0;
size_t RxCounter = 0;
size_t counti;
uint8_t sbuf[12000];
//发送函数
void send(){
  if(Serial.available())
  {
    if(RxHeadState == 0&&Serial.read()==0x00)
    {
      RxHeadState = 1;
    }
    else if(RxHeadState == 1&&Serial.read()==0xff)
    {
      RxHeadState = 2;
    }
    else if(RxHeadState == 2&&Serial.read()==0x01)
    {
      RxHeadState = 3;
    }
    else if(RxHeadState == 3&&Serial.read()==0x01)
    {
      RxHeadState = 4;
    }
    else if(RxHeadState == 4)
    {
      sbuf[RxCounter++]=Serial.read();
      if(RxTailState == 0&&sbuf[RxCounter-1]==0x01)
      {
        RxTailState = 1;
      }
      else if(RxTailState == 1&&sbuf[RxCounter-1]==0x01)
      {
        RxTailState = 2;
      }
      else if(RxTailState == 2&&sbuf[RxCounter-1]==0xff)
      {
        RxTailState = 3;
      }
      else if(RxTailState == 3&&sbuf[RxCounter-1]==0x00)
      {

        RxTailState = 4;
      }
      else if(RxCounter>6400)
      {
        if(Serial.available())
        {
          counti = Serial.available();
          Serial.readBytes(sbuf, counti);
        }
        RxHeadState = 0;
        RxTailState = 0;
        RxCounter=0;
      }
      else
      {
        RxTailState = 0;
      }
    }
    else
    {
      if(Serial.available())
      {
        counti = Serial.available();
        Serial.readBytes(sbuf, counti);
      }
      RxHeadState = 0;
    }
  }
  if(RxTailState == 4)
  {
    counti=RxCounter-4;
    Udp.beginPacket(STAip, 8080);//只需对此处进行修改，需要发送的ip及端口
    Udp.write(0x00);
    Udp.write(0xff);
    Udp.write(0x01);
    Udp.write(0x01);
    Udp.endPacket();
    uint8_t a = 0;
    for(size_t i=0;i<counti;i=i+1440)
    {
      a = a+1;
      if(counti-i>=1440)
      {
        Udp.beginPacket(STAip, 8080);//只需对此处进行修改，需要发送的ip及端口
        Udp.write(sbuf+i,1440);
        Udp.endPacket();
      }
      else
      {
        Udp.beginPacket(STAip, 8080);//只需对此处进行修改，需要发送的ip及端口
        Udp.write(sbuf+i,counti-i);
        Udp.endPacket();
      }
    }
    Udp.beginPacket(STAip, 8080);//只需对此处进行修改，需要发送的ip及端口
    Udp.write(0x01);
    Udp.write(0x01);
    Udp.write(0xff);
    Udp.write(0x00);
    Udp.endPacket();
    RxHeadState = 0;
    RxTailState = 0;
    RxCounter=0;

    if(Serial.available())
    {
      counti = Serial.available();
      Serial.readBytes(sbuf, counti);
    }
  }
}
 
void loop() {
  send();
}